package com.qianfeng.wang.guangjie2.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.handmark.pulltorefresh.library.internal.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.wang.guangjie2.R;
import com.qianfeng.wang.guangjie2.adapter.ShowViewPagerAdapter;
import com.qianfeng.wang.guangjie2.config.AppConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gw555 on 2015/7/6.
 */
public class ShowcaseFragment extends AppBaseFragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    //viewpager
    @ViewInject(R.id.showViewPager)
    private ViewPager showViewPager;
    private ShowViewPagerAdapter showViewPagerAdapter;
    private List<Fragment> fragments;
    //RadioGroup
    @ViewInject(R.id.rg_showcase_radiogroup)
    private RadioGroup radioGroup;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.showcase_fragment, container, false);
        ViewUtils.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        showViewPager = new ViewPager(getActivity());
        fragments = new ArrayList<>();
        fragments.add(new ShViewPagerFragment(AppConfig.ONLINESTORE));
        fragments.add(new ShViewPagerFragment(AppConfig.SOLIDSTORE));
        showViewPagerAdapter = new ShowViewPagerAdapter(getChildFragmentManager(), fragments);
        showViewPager.setAdapter(showViewPagerAdapter);
        //viewpager点击事件
        showViewPager.setOnPageChangeListener(this);

        //RadioGroup点击事件
        radioGroup.setOnCheckedChangeListener(this);

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {//页面滑动

    }

    @Override
    public void onPageSelected(int position) {//页面选择
        RadioButton rb = (RadioButton) radioGroup.getChildAt(position);
        rb.setChecked(true);

    }

    @Override
    public void onPageScrollStateChanged(int state) {//页面状态改变

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_showcase_online:
                showViewPager.setCurrentItem(0);
            case R.id.rb_showcase_physical:
                showViewPager.setCurrentItem(1);
        }
    }
}
